MySQL's Procedural SQL (also known as MySQL stored programs) allows you to write logic-driven, reusable code inside the database itself. Unlike basic SQL (which is declarative), procedural SQL introduces:
This makes database-side programming more powerful and modular.
Variables are used to store values temporarily in memory. You must declare them using DECLARE and assign using SET.
DECLARE v_salary INT; SET v_salary = 45000;
Control structures allow conditional and repetitive execution of code. MySQL supports:
IF condition THEN -- action ELSE -- alternative action END IF;
Used for branching based on a condition.
CASE grade WHEN 'A' THEN SET remark = 'Excellent'; WHEN 'B' THEN SET remark = 'Good'; ELSE SET remark = 'Poor'; END CASE;
Used for multi-condition branching (like switch-case).
label: LOOP -- statements LEAVE label; -- to exit the loop END LOOP;
A basic loop that must be manually exited using LEAVE.
WHILE condition DO -- repeat END WHILE;
Runs as long as condition is TRUE.
REPEAT -- code UNTIL condition END REPEAT;
Runs the block at least once, then checks condition.
A stored procedure is a named set of SQL statements saved in the database. It performs an action but does not return a value directly (though OUT parameters can be used).
CREATE PROCEDURE proc_name (IN param1 INT) BEGIN -- statements END;
A stored function is like a procedure but it returns a single value directly using RETURN.
CREATE FUNCTION func_name(param INT) RETURNS INT BEGIN RETURN param * 2; END;
You can now use:
SELECT func_name(10); -- returns 20
A trigger is a block of code that automatically runs in response to events like INSERT, UPDATE, or DELETE.
CREATE TRIGGER before_insert BEFORE INSERT ON users FOR EACH ROW BEGIN SET NEW.created_at = NOW(); END;
A cursor is a pointer that allows row-by-row processing of query results.
DECLARE done INT DEFAULT 0; DECLARE emp_name VARCHAR(50); DECLARE cur CURSOR FOR SELECT name FROM employees; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; OPEN cur; read_loop: LOOP FETCH cur INTO emp_name; IF done THEN LEAVE read_loop; END IF; -- Process each employee END LOOP; CLOSE cur;
MySQL lets you handle exceptions using DECLARE HANDLER statements.
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET @error_message = 'Something went wrong';
Procedures can accept and return parameters:
CREATE PROCEDURE add_numbers(IN a INT, IN b INT, OUT sum INT) BEGIN SET sum = a + b; END;
To call:
CALL add_numbers(5, 3, @result); SELECT @result;
An event is a scheduled task run at a specific time or interval.
CREATE EVENT delete_old_logs ON SCHEDULE EVERY 1 DAY DO DELETE FROM logs WHERE created_at < NOW() - INTERVAL 30 DAY;
Don't forget: SET GLOBAL event_scheduler = ON;
The DELIMITER command is not SQL, but a MySQL client instruction used to change the end-of-statement marker so you can define complex blocks (which may include ; inside them).
DELIMITER // CREATE PROCEDURE my_proc() BEGIN SELECT 'Hello'; END; // DELIMITER ;
Feature | Procedure | Function |
---|---|---|
Return Value | No (uses OUT) | Yes (RETURN keyword) |
Use in SELECT | No | Yes |
Purpose | Perform task | Compute and return a value |
Call syntax | CALL proc() | SELECT func() |